#!/bin/bash
#
# 抖音关注：程序员三丙
# 知识星球：https://t.zsxq.com/j9b21
#

set -e

clickhouse-client --user sanbing --password sanbing --query "
CREATE DATABASE IF NOT EXISTS example;

USE example;

DROP TABLE IF EXISTS like_record;

CREATE TABLE IF NOT EXISTS like_record (
    id UUID,
    v_id UUID,
    user_id String,
    create_time DateTime('Asia/Shanghai'),
    field_1 String,
    field_2 String,
    field_3 String,
    field_4 String,
    field_5 String,
    field_6 String,
    field_7 String,
    field_8 String,
    field_9 String,
    field_10 String
) ENGINE = MergeTree()
      ORDER BY (v_id, user_id, create_time)
      PARTITION BY formatDateTime(create_time,'%Y_%m_%d')
      SETTINGS index_granularity = 8192
;


DROP TABLE IF EXISTS like_record_kafka;

CREATE TABLE IF NOT EXISTS like_record_kafka (
    id UUID,
    v_id UUID,
    user_id String,
    create_time DateTime('Asia/Shanghai'),
    field_1 String,
    field_2 String,
    field_3 String,
    field_4 String,
    field_5 String,
    field_6 String,
    field_7 String,
    field_8 String,
    field_9 String,
    field_10 String
) ENGINE = Kafka
SETTINGS kafka_broker_list = 'kafka:9092',
   kafka_topic_list = 'like_record_ck',
   kafka_group_name = 'like_record_consumer',
   kafka_format = 'JSONEachRow',
   kafka_skip_broken_messages = 10,
   kafka_num_consumers = 10,
   kafka_thread_per_consumer = 1,
   kafka_poll_max_batch_size = 1048576,
   kafka_poll_timeout_ms = 100,
   kafka_max_block_size = 1048576;


DROP TABLE IF EXISTS like_record_mv;

CREATE MATERIALIZED VIEW like_record_mv TO like_record
AS
SELECT
    id,
    v_id,
    user_id,
    create_time,
    field_1,
    field_2,
    field_3,
    field_4,
    field_5,
    field_6,
    field_7,
    field_8,
    field_9,
    field_10
FROM
    like_record_kafka;
"